package com.tjsinfo.tjsoa.documentationflow.xingzhengfawen.action;

import java.io.IOException;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import jxl.write.WriteException;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;
import org.json.JSONArray;
import org.json.JSONObject;

import com.tjsinfo.tjsoa.documentationflow.xingzhengfawen.bo.IFaWenAdminBO;
import com.tjsinfo.tjsoa.documentationflow.xingzhengfawen.bo.IWenTypeBO;
import com.tjsinfo.tjsoa.documentationflow.xingzhengfawen.vo.SubTypeInfo;
import com.tjsinfo.tjsoa.system.dao.ISystemSpringDao;

public class DengJiBuAction extends DispatchAction{


	private ISystemSpringDao springDao;
	private IWenTypeBO wenTypeBO;
	
	public IWenTypeBO getWenTypeBO() {
		return wenTypeBO;
	}
	
	
	public void setWenTypeBO(IWenTypeBO wenTypeBO) {
		this.wenTypeBO = wenTypeBO;
	}


	public ISystemSpringDao getSpringDao() {
		return springDao;
	}

	public void setSpringDao(ISystemSpringDao springDao) {
		this.springDao = springDao;
	}

	public ActionForward dengjibuSearch(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) throws IOException, WriteException{
//		jxl.write.WritableWorkbook wwb = null;
		try {
			
			String beginNO=request.getParameter("beginNO");
			String endNO=request.getParameter("endNO");
			String doctype = request.getParameter("doctype");
			String itemtype = request.getParameter("itemtype");
			String whereSQL=" fawendatetime like '%"+request.getParameter("yearNO")+"%' and flowid <> 0 and ";
			if (beginNO!=null && !"".equals(beginNO)) {
				whereSQL+="wenhaonumber>="+beginNO+" and ";
			}
			if (endNO !=null && !"".equals(endNO)) {
				whereSQL+="wenhaonumber<="+endNO+" and ";
			}
			if (doctype !=null && !"".equals(doctype)) {
				SubTypeInfo subtype=wenTypeBO.selectSubTypeById(Integer.valueOf(doctype)).get(0);
				whereSQL+="wentype='"+subtype.getTypeName() +"' and ";	
			}
			JSONArray array = new JSONArray();
			JSONObject object = new JSONObject();
			String tablename = "t_xingzhengfawen_neirong";
			if (!"1".equals(itemtype)){
				tablename = "t_dangweifawen_neirong";
			}
			List<Map<String, String>> tempList = springDao.commonSelect(new String[]{"wenhaotype","fawendatetime","zhusong","biaoti","nigaorenname","fenshu","wenhaonumber"}, "SELECT wenhaotype,fawendatetime,zhusong,biaoti,nigaorenname,fenshu,wenhaonumber FROM "+ tablename +" where "+whereSQL+" fawendatetime is not null order by wenhaonumber");
			for (Map<String, String> map : tempList) {
				JSONObject json = new JSONObject();
				json.put("wenhaotype", map.get("wenhaotype"));
				json.put("fawendatetime", map.get("fawendatetime").substring(0, 10));
				
				json.put("zhusong", map.get("zhusong"));
				json.put("biaoti", map.get("biaoti"));
				json.put("nigaorenname", map.get("nigaorenname"));
				json.put("fenshu", map.get("fenshu"));
				array.put(json);
			}

			object.put("root", array);
			response.getWriter().print(object);

			return null;
		} catch (Exception e) {

			e.printStackTrace();
			return null;
		}
		
	}
}
